// pages/order/order.js
const globalIp = getApp().globalData.userIp
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    swipertab: [{ name: '已下单', index: 0 }, { name: '备货中', index: 1 }, { name: '已完成', index: 2 }],
    pageIndex:1,
    pageSize:8,
    total: 0,
    alreadyOrder: [],
    waitPayOrder: [],
    lostOrder: []
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo()
    this.orderShow()
  },
 
  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },
 
  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },
 
  tabChange: function (e) {
    this.setData({ 
      currtab: e.detail.current,
      pageIndex: 1, // 初始化
      total: 0
    })
    this.orderShow()
  },
 
  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        that.alreadyShow()
        break
      case 1:
        that.waitPayShow()
        break
      case 2:
        that.lostShow()
        break
    }
  },
  alreadyShow: function(){
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: globalIp + 'home/ordr/execOrder',
      data: {
        pageSize: this.data.pageSize,
        pageIndex: this.data.pageIndex,
        value: 1,
        openId: wx.getStorageSync('userInfo').openid,
        isEmp: wx.getStorageSync('userInfo').role,
        userName: wx.getStorageSync('userInfo').role == 'U'
          ? wx.getStorageSync('userInfo').CardCode 
          : wx.getStorageSync('userInfo').empCode
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        that.setData({
          total: that.data.total + res.data.total,
          alreadyOrder: [...that.data.alreadyOrder, ...res.data.jsonObject.data]
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
 
  waitPayShow:function(){
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: globalIp + 'home/ordr/execOrder',
      data: {
        pageSize: this.data.pageSize,
        pageIndex: this.data.pageIndex,
        value: 2,
        openId: wx.getStorageSync('userInfo').openid,
        isEmp: wx.getStorageSync('userInfo').role,
        userName: wx.getStorageSync('userInfo').role == 'U'
          ? wx.getStorageSync('userInfo').CardCode 
          : wx.getStorageSync('userInfo').empCode
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        that.setData({
          total: that.data.total + res.data.total,
          waitPayOrder: [...that.data.waitPayOrder, ...res.data.jsonObject.data]
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
 
  lostShow: function () {
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: globalIp + 'home/ordr/execOrder',
      data: {
        pageSize: this.data.pageSize,
        pageIndex: this.data.pageIndex,
        value: 3,
        openId: wx.getStorageSync('userInfo').openid,
        isEmp: wx.getStorageSync('userInfo').role,
        userName: wx.getStorageSync('userInfo').role == 'U'
          ? wx.getStorageSync('userInfo').CardCode 
          : wx.getStorageSync('userInfo').empCode
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        that.setData({
          total: that.data.total + res.data.total,
          lostOrder: [...that.data.lostOrder, ...res.data.jsonObject.data]
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function(res) {
    this.setData({
      pageIndex: this.data.pageIndex + 1
    })
    let that = this
    switch (this.data.currtab) {
      case 0:
        that.alreadyShow()
        break
      case 1:
        that.waitPayShow()
        break
      case 2:
        that.lostShow()
        break
    }
  }
  
})